﻿using RightControl.Common;
using RightControl.IService;
using RightControl.Model;
using RightControl.WebApp.Areas.Admin.Controllers;
using System;
using System.Web.Mvc;

namespace RightControl.WebApp.Areas.Permissions.Controllers
{
    public class UserController : BaseController {
        public IUserService userService { get; set; }
        public IRoleService roleService { get; set; }
        public SelectList RoleList { get { return new SelectList(roleService.GetRoleList(), "Id", "RoleName"); } }
        // GET: Permissions/User
        public ActionResult Index(int? id) {
            ViewBag.RoleId = RoleList;
            base.Initialize(id);
            return View();
        }
        /// <summary>
        /// 加载数据列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="pageInfo"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult List(UserModel filter, PageInfo pageInfo) {
            var result = userService.GetListByFilter(filter, pageInfo);
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Detail(int Id) {
            var model = userService.GetDetail(Id);
            return View(model);
        }
        public ActionResult Edit(int Id) {
            var model = userService.GetDetail(Id);
            ViewBag.RoleId = RoleList;
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(UserModel model) {
            model.UpdateOn = DateTime.Now;
            model.UpdateBy = Operator.UserId;
            var result = userService.UpdateModel(model) ? SuccessTip() : ErrorTip();
            return Json(result);
        }
        public ActionResult Add() {
            ViewBag.RoleId = RoleList;
            return View();
        }
        [HttpPost]
        public ActionResult Add(UserModel model) {
            model.CreateOn = DateTime.Now;
            model.CreateBy = Operator.UserId;
            model.PassWord = Md5.md5(Configs.GetValue("InitUserPwd"), 32);
            var result = userService.CreateModel(model) ? SuccessTip() : ErrorTip();
            return Json(result);
        }
        [HttpPost]
        public ActionResult Delete(int Id) {
            var result = userService.DeleteModel(Id) ? SuccessTip() : ErrorTip();
            return Json(result);
        }
        [HttpPost]
        public ActionResult InitPwd(int Id) {
            var initPwd = Md5.md5(Configs.GetValue("InitUserPwd"), 32);
            UserModel model = new UserModel { Id = Id, PassWord = initPwd };
            var result = userService.InitPwd(model) ? SuccessTip() : ErrorTip();
            return Json(result);
        }


        [HttpPost]
        public JsonResult SwitchUser(string username) {
            if (Operator.UserId == 1) {
                UserModel userEntity = userService.CheckLogin(username, "");
                if (userEntity != null) {
                    OperatorModel operatorModel = new OperatorModel();
                    operatorModel.UserId = userEntity.Id;
                    operatorModel.UserName = userEntity.UserName;
                    operatorModel.RealName = userEntity.RealName;
                    operatorModel.RoleId = userEntity.RoleId;
                    operatorModel.HeadShot = userEntity.HeadShot;
                    operatorModel.LoginIPAddress = Net.Ip;
                    operatorModel.LoginIPAddressName = Net.GetLocation(operatorModel.LoginIPAddress);
                    operatorModel.LoginTime = DateTime.Now;
                    operatorModel.LoginToken = DESEncrypt.Encrypt(Guid.NewGuid().ToString());
                    operatorModel.IsSystem = userEntity.UserName == "admin";
                    OperatorProvider.Provider.AddCurrent(operatorModel);

                    userEntity.LastLoginTime = DateTime.Now;
                    userService.UpdateModel(userEntity);

                    return Json(SuccessTip("登录成功"), JsonRequestBehavior.AllowGet);
                }
            }
            return Json(ErrorTip("切换失败"), JsonRequestBehavior.AllowGet);
        }
    }
}